<?php

// +----------------------------------------------------------------------
// | 添加修改商品分类
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\seller\event;

use app\common\event\Base;
use think\Exception;
use app\seller\model\Store;
use app\common\model\GoodClass as GoodClassModel;
use app\common\model\GoodSpec as GoodSpecModel;
use think\Db;

class GoodSpec extends Base
{

    protected $post;
    protected $good_class;
    protected $good_spec;
    protected $num = 0;
    /**
     * @var Store
     */
    protected $store;
    public function update($post,$store)
    {
        Db::startTrans();
        try {
            $this->post = $post;
            $this->store = $store;
            if (!isset($post['good_class_id'])) {
                throw new Exception('缺少分类ID',300300);
            }
            $this->good_class = GoodClassModel::get($post['good_class_id']);
            if (!isset($post['good_class_type_spec_id'])) {
                throw new Exception('缺少分类规格ID',300305);
            }
            $this->good_spec = GoodSpecModel::get($post['good_class_type_spec_id']);
            if (!$this->good_class) {
                //TODO:商家权限验证
                throw new Exception('商品分类不存在或者无权限',300301);
            }
            // 数据更新
            $this->_edit();
            // 数据添加
            $this->_add();
            // 提交事务
            Db::commit();
            return $this->num;
        } catch (Exception $e) {
                // 回滚事务
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
    // 数据修改
    protected function _edit()
    {
        if (isset($this->post['edit'])) {
            foreach ($this->post['edit'] as $edit) {
                $value = $this->good_spec->value()->find($edit['id']);
                if ($value && $value['store_id'] == $this->store['id']) {
                    if (1 == $edit['del']) {
                        //用户移除;
                        $value->status = -2;
                        $this->num += $value->save();
                    }else {
                        //更新
                        $value->sort = $edit['sort'];
                        $value->name = $edit['name'];
                        $this->num += $value->save();
                    }
                }else {
                    throw new Exception('分类类型规格值不存在或者无权限',300302);
                }
            };
        }
    }
    // 数据增加
    protected function _add()
    {
        if (isset($this->post['add'])) {
            foreach ($this->post['add'] as $add) {
                $data['name'] = $add['name'];
                $data['sort'] = $add['sort'];
                $data['store_id'] = $this->store['id'];
                $data['good_class_id'] = $this->good_class['id'];
                $this->good_spec->value()->save($data);
                $this->num ++;
            };
        }
    }
}
